export const withInstall = (component, alias) => {
  const comp = component
  comp.install = (app) => {
    app.component(comp.name || comp.displayName, component)
    if (alias) {
      app.config.globalProperties[alias] = component
    }
  }
  return component
}
export const getAssetsImages = (name) => {
  return new URL(`/src/assets/image/${name}`, import.meta.url).href
}

export const getServerImages = (name) => {
  return name ? `${import.meta.env.VITE_APP_BASE_API}/system/sysAttach/getImage?filePath=${name}` : ``
}

/**
 * let headers = {};
      headers[setting.tokenHeaderName] =setting.takeToken();
      axios({
        url: '/hr/xxxuser/export',
        method: 'get',
        responseType: 'blob',
        params: e,
        headers: headers
      }).then((res) => {
        resolveBlob(res);
      }).catch((err) => {
        console.log('err',err);
      })
 *
 * @export
 * @param {*} res
 */
export function resolveBlob(res) {
  if (res && res.data && res.data.type == 'application/json') {
    const reader = new FileReader()
    reader.readAsText(res.data, 'utf-8')
    reader.onload = () => {
      let rr = JSON.parse(reader.result)
      message.error(rr.message)
    }

    return
  }

  const aLink = document.createElement('a')
  var blob = new Blob([res.data], {})
  // //从response的headers中获取filename, 后端response.setHeader("Content-disposition", "attachment; filename=xxxx.docx") 设置的文件名;
  var patt = new RegExp('filename=([^;]+\\.[^\\.;]+);*')
  var contentDisposition = decodeURI(res.headers['content-disposition'])

  var result = patt.exec(contentDisposition)
  if (result != null) {
    var fileName = decodeURIComponent(result[1])
    fileName = fileName.replace(/"/g, '')
    fileName = fileName.replace(/\+/g, ' ')
    aLink.href = URL.createObjectURL(blob)
    aLink.setAttribute('download', fileName) // 设置下载文件名称
    document.body.appendChild(aLink)
    aLink.click()
    document.body.appendChild(aLink)
  }
}

export function eachTreeData(data, callback, childrenField = 'children', parent) {
  if (data) {
    data.forEach((d, i) => {
      var _a
      if (callback && callback(d, i, parent) !== false && ((_a = d[childrenField]) == null ? void 0 : _a.length)) {
        eachTreeData(d[childrenField], callback, childrenField, d)
      }
    })
  }
}
